package com.example.service.impl;

import com.example.dao.StudentDao;
import com.example.po.Student;
import com.example.po.User;
import com.example.service.StudentService;
import com.example.utils.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao studentDao;
    @Override
    public List<Student> findStudents(Student student,HttpSession session) {
        Page page= (Page) session.getAttribute("page");
        if(page==null){
            page=new Page();
            page.setCount(10);
            page.setStart(1);
            page.setTotal(studentDao.findStudentsCount(student));
        }
        session.setAttribute("page",page);
        PageHelper.startPage(page.getStart(),page.getCount());
        List<Student> students=studentDao.findStudents(student);
        return students;
    }

    @Override
    public List<Student> searchStudentById(Student student) {
        return studentDao.findStudents(student);
    }

    @Override
    public boolean updateStudent(Student student) {
        if(studentDao.updateStudent(student)>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public String addStudent(Student student) {
        Student student1 = studentDao.selectid(student);
        if(student1==null){
            System.out.println("=========");
            int i = studentDao.addStudent(student);
            System.out.println(i);
            if(i!=0){
                return "OK";
            }else {
                return "fail";
            }
        }else{
            return  "error";
        }
    }
    @Override
    public boolean deleteStudent(int id ) {
        System.out.println(id);
        int i = studentDao.deleteStudent(id);
        System.out.println(i);
        if(i>0){
            return true;
        }else{
            return false;
        }
    }
    @Override
    public List<Student> searchStudent() {
        return studentDao.searchStudent();
    }

}
